package com.tangcco.lockwell;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.tangcco.lockwell.context.WebContext;
import com.tangcco.lockwell.domain.Article;

import com.tangcco.lockwell.domain.User;
import com.tangcco.lockwell.repository.GroupsMapper;
import com.tangcco.lockwell.repository.GroupsRelationshipMapper;
import com.tangcco.lockwell.repository.UserEventLogMapper;
import com.tangcco.lockwell.repository.UserMapper;
import com.tangcco.lockwell.service.ArticleService;
import com.tangcco.lockwell.service.QuestionService;
import com.tangcco.lockwell.service.TextContentService;
import com.tangcco.lockwell.service.UserEventLogService;
import com.tangcco.lockwell.service.impl.DataDictionaryServiceImpl;
import com.tangcco.lockwell.test.*;
import com.tangcco.lockwell.utility.JsonUtil;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

import java.io.File;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.tangcco.lockwell.utility.DateToString.dateToString;

@SpringBootTest
class LockwellApplicationTests_xmy {
    @Resource
    QuestionService questionService;
    @Resource
    ArticleService articleService;

    @Resource
    UserEventLogService userEventLogService;
    @Resource
    UserEventLogMapper userEventLogMapper;
    @Autowired
    WebContext webContext;


    //接收文件并输出  应该是测试是否接收成功的并查看数据
    @Test
    void contextLoads6() throws IOException {
/*        String jsonFilePath = "D:\\paper\\yy2_2024.json";
        ObjectMapper objectMapper = new ObjectMapper();

        List<Questionnaire> questionnaires = objectMapper.readValue(new File(jsonFilePath), objectMapper.getTypeFactory().constructCollectionType(List.class, Questionnaire.class));
        // 检查列表是否为空，并打印第一个元素
//        System.out.println(questionnaires.get(0));
        Questionnaire questionnaire = questionnaires.get(0);
        List<Material> materials = questionnaire.getMaterials();
        for (Material material : materials) {
            Content content = material.getContent().get(0);
            String contenttext = "";
            List<String> value = content.getValue();
            for (String s : value) {
                contenttext = contenttext + s + "\n";
            }
            System.out.println(contenttext);
            Article article = new Article();
            //手动给ArticleId
            //article.setArticleId(num);
            article.setArticleContent(contenttext);
            article.setCreateTime(dateToString(new Date()));
            articleService.inserts(article);
            List<Question> questions = material.getQuestions();

            List<Q1> q1s = new ArrayList<>();
            List<String> correctAnswer = new ArrayList<>();
            for (Question q : questions) {

                Q1 q1 = new Q1();
                q1.setQtitle(q.getQuestion_title());
                List<String> options = q.getOptions();
                List<Q3> q3s = new ArrayList<>();
                if (q.getCorrectAnswer().equals("0")) correctAnswer.add("A");
                if (q.getCorrectAnswer().equals("1")) correctAnswer.add("B");
                if (q.getCorrectAnswer().equals("2")) correctAnswer.add("C");
                if (q.getCorrectAnswer().equals("3")) correctAnswer.add("D");
                if (q.getCorrectAnswer().equals("4")) correctAnswer.add("E");
                if (q.getCorrectAnswer().equals("5")) correctAnswer.add("F");
                if (q.getCorrectAnswer().equals("6")) correctAnswer.add("G");
                int i = 1;
                for (String option : options) {
                    Q3 q3 = new Q3();
                    if (i == 1) q3.setPrefix("A");
                    if (i == 2) q3.setPrefix("B");
                    if (i == 3) q3.setPrefix("C");
                    if (i == 4) q3.setPrefix("D");
                    if (i == 5) q3.setPrefix("E");
                    if (i == 6) q3.setPrefix("F");
                    if (i == 7) q3.setPrefix("G");
                    i++;
                    q3.setContent(option);
                    q3s.add(q3);
                }
                q1.setItem(q3s);
                q1s.add(q1);
            }
            Integer artId = article.getArticleId();
            String cont = JsonUtil.toJsonStr(q1s);
            String corr = String.join(",", correctAnswer);
            int subjectid = 5;
            int subjectparentid = 1;
            int year = 2024;
            int status = 1;

            int type = material.getType();
            if (type == 9) type = 22;
            if (type == 3) type = 20;
            if (type == 13) type = 40;
            if (type == 203) type = 21;
            if (type == 207) type = 23;
            if (type == 205) type = 24;
            com.tangcco.lockwell.domain.Question question1 = new com.tangcco.lockwell.domain.Question();
            question1.setArticleContentId(artId);
            question1.setQuestionType(type);
            question1.setTextContent(cont);
            question1.setCorrect(corr);
            question1.setCreateTime(dateToString(new Date()));
            question1.setSubjectId(subjectid);
            question1.setSubjectParentId(subjectparentid);
            question1.setGradeLevel(year);
            question1.setCreateUser(String.valueOf(18));
            question1.setStatus(status);
            questionService.inserts(question1);
        }

        List<Material> writing = questionnaire.getWriting();
        for (Material material : writing) {
            Content content = material.getContent().get(0);
            String contenttext = "";
            List<String> value = content.getValue();
            for (String s : value) {
                contenttext = contenttext + s + "\n";
            }
            System.out.println(contenttext);
            Article article = new Article();
            //手动给ArticleId
            //article.setArticleId(num);
            article.setArticleContent(contenttext);
            article.setCreateTime(dateToString(new Date()));
            articleService.inserts(article);
            List<Question> questions = material.getQuestions();

            List<Q1> q1s = new ArrayList<>();
            List<String> correctAnswer = new ArrayList<>();

            Integer artId = article.getArticleId();
            String cont = JsonUtil.toJsonStr(q1s);
            String corr = String.join(",", correctAnswer);
            int subjectid = 5;
            int subjectparentid = 1;
            int year = 2024;
            int status = 1;

            int type = material.getType();
            if (type == 207) type = 23;
            if (type == 205) type = 24;
            com.tangcco.lockwell.domain.Question question1 = new com.tangcco.lockwell.domain.Question();
            question1.setArticleContentId(artId);
            question1.setQuestionType(type);
            question1.setTextContent(cont);
            question1.setCorrect(corr);
            question1.setCreateTime(dateToString(new Date()));
            question1.setSubjectId(subjectid);
            question1.setSubjectParentId(subjectparentid);
            question1.setGradeLevel(year);
            question1.setCreateUser(String.valueOf(18));
            question1.setStatus(status);

            questionService.inserts(question1);
        }
    }
*/
//        int i = userEventLogService.selectCountByUserName();
//        System.out.println(i);
    }
}
